/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.ruralLibrary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.dto.UserInfo;
import com.pig4cloud.pigx.admin.api.entity.SysRole;
import com.pig4cloud.pigx.admin.api.entity.SysUserRole;
import com.pig4cloud.pigx.admin.api.feign.RemoteDataScopeService;
import com.pig4cloud.pigx.admin.api.feign.RemoteUserService;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.ruralLibrary.api.dto.ResourceAuditDto;
import com.pig4cloud.pigx.ruralLibrary.api.entity.BookUserEntity;
import com.pig4cloud.pigx.ruralLibrary.api.entity.ResourceAudit;
import com.pig4cloud.pigx.ruralLibrary.controller.ResourceAuditController;
import com.pig4cloud.pigx.ruralLibrary.mapper.ResourceAuditMapper;
import com.pig4cloud.pigx.ruralLibrary.service.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源
 *
 * @author pig
 * @date 2023-10-31 14:34:33
 */
@Service
@Slf4j
public class ResourceAuditServiceImpl extends ServiceImpl<ResourceAuditMapper, ResourceAudit> implements ResourceAuditService {

	private final Logger logger = LoggerFactory.getLogger(ResourceAuditServiceImpl.class);

	@Autowired
	private BookUserService bookUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleService sysRoleService;

	@Autowired(required = false)
	private RemoteDataScopeService remoteDataScopeService;

    @Override
    public R pageList(Page page, LambdaQueryWrapper<ResourceAudit> wrapper) {
		logger.info("=======当前登录用户的角色信息={}",SecurityUtils.getRoles());
		R<List<SysRole>> listR=remoteDataScopeService.getRoleList(SecurityUtils.getRoles().stream()
				.map(String::valueOf)
				.collect(Collectors.toList()));
		if (listR != null) {
			if ("PROVINCIAL_ROLE".equals(listR.getData().get(0).getRoleCode())) {
				wrapper.eq(ResourceAudit::getProvince, "41");
			}
			if ("MUNICIPAL_ROLE".equals(listR.getData().get(0).getRoleCode())) {
				BookUserEntity bookUser = bookUserService.getById(SecurityUtils.getUser().getId());
				if (bookUser != null) {
					wrapper.eq(ResourceAudit::getCity, bookUser.getCity());
				}
			}
			if ("DISTRICT_ROLE".equals(listR.getData().get(0).getRoleCode())) {
				BookUserEntity bookUser = bookUserService.getById(SecurityUtils.getUser().getId());
				if (bookUser != null) {
					wrapper.eq(ResourceAudit::getArea, bookUser.getDistrict());
				}
			}
		}
		wrapper.orderByDesc(ResourceAudit::getCreateTime);
		Page<ResourceAudit> paged = this.page(page, wrapper);
        return R.ok(paged);
    }

	@Override
	public R detail(Long id) {
		return R.ok(this.getById(id));
	}

	@Override
	public R audit(ResourceAuditDto dto) {
		logger.info("=======当前登录用户的角色信息={}",SecurityUtils.getRoles());
		logger.info("=======当前审核资源id={}",dto.getIds());
		R<List<SysRole>> listR=remoteDataScopeService.getRoleList(SecurityUtils.getRoles().stream()
				.map(String::valueOf)
				.collect(Collectors.toList()));
		logger.info("=======当前角色={}",listR);
		LambdaQueryWrapper<ResourceAudit> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ResourceAudit::getResourceId,dto.getIds());
		List<ResourceAudit> resourceAuditList=this.list(wrapper);
		resourceAuditList.forEach(resourceAudit -> {
			if (listR != null) {
				if ("PROVINCIAL_ROLE".equals(listR.getData().get(0).getRoleCode())) {
					resourceAudit.setAuditStatus("3");
				}
				if ("MUNICIPAL_ROLE".equals(listR.getData().get(0).getRoleCode())) {
					resourceAudit.setAuditStatus("2");
				}
				if ("DISTRICT_ROLE".equals(listR.getData().get(0).getRoleCode())) {
					resourceAudit.setAuditStatus("1");
				}
				if ("ROLE_ADMIN".equals(listR.getData().get(0).getRoleCode())) {
					resourceAudit.setAuditStatus("3");
				}
			}
			resourceAudit.setAuditPerson(SecurityUtils.getUser().getName());
			resourceAudit.setAuditTime(new Date());
		});
		return  R.ok(this.updateBatchById(resourceAuditList));
	}


}
